package com.litidea.dao.impl;

import static com.litidea.commons.dict.StaticDics.OK_MAP;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.litidea.commons.utils.JSONUtils;
import com.litidea.dao.BasicDataDAO;
import com.litidea.dao.abs.BasicDataDAOAbs;
import com.litidea.dataobjects.ChangeTypeDO;
import com.litidea.dataobjects.ChangeTypeTestItemsDO;
import com.litidea.dataobjects.TestItemDO;

@SuppressWarnings("unchecked")
public class BasicDataDAOImpl extends BasicDataDAOAbs implements BasicDataDAO {

	/**
	 * 获取测试项目数量
	 * 
	 * @param params
	 * @return
	 */
	private Integer getTestItemsCount(Map<String, String> params) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				"basicData.getTestItemsCount", params);
	}
	
	/**
	 * 获取变更类型数量
	 * 
	 * @param params
	 * @return
	 */
	private Integer getChangeTypesCount(Map<String, String> params) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				"basicData.getChangeTypesCount", params);
	}

	@Override
	public List<TestItemDO> getTestItems(Map<String, String> params) {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList(
				"basicData.getTestItems", params);
	}

	@Override
	public JSONObject getTestItemsPageList(Map<String, String> params) {
		// TODO Auto-generated method stub
		List<TestItemDO> list = this.getSqlMapClientTemplate().queryForList(
				"basicData.getTestItems", params);
		return JSONUtils.getPageJSON(list, this.getTestItemsCount(params));
	}

	@Override
	public String addNewTestItem(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().insert("basicData.addNewTestItems",
				params);
		return "添加新测试项目成功";
	}

	@Override
	public String delTestItem(Integer id) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().delete("basicData.deleteTestItem", id);
		return "删除测试项目成功";
	}

	@Override
	public String editTestItem(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate()
				.update("basicData.updateTestItem", params);
		return "编辑测试项目成功";
	}
	
	@Override
	public List<ChangeTypeDO> getChangeTypes(Map<String, String> params) {
		// TODO Auto-generated method stub
		List<ChangeTypeDO> list = this.getSqlMapClientTemplate().queryForList(
				"basicData.getChangeTypes", params);
		return list;
	}

	@Override
	public JSONObject getChangeTypesPageList(Map<String, String> params) {
		// TODO Auto-generated method stub
		List<ChangeTypeDO> list = this.getSqlMapClientTemplate().queryForList(
				"basicData.getChangeTypes", params);
		
		for(ChangeTypeDO changeTypeDO:list){
			changeTypeDO.setIsPublishedString(OK_MAP.get(changeTypeDO.getIsPublished()));
		}
		
		return JSONUtils.getPageJSON(list, this.getChangeTypesCount(params));
	}

	@Override
	public String addNewChangeType(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().insert("basicData.addNewChangeType",
				params);
		return "添加新变更类型成功";
	}

	@Override
	public String delChangeType(Integer id) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().delete("basicData.deleteChangeType", id);
		return "删除变更类型成功";
	}

	@Override
	public String editChangeType(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate()
				.update("basicData.updateChangeType", params);
		return "编辑变更类型成功";
	}

	@Override
	public List<TestItemDO> getChangeTypeTestItemss(Integer changeId) {
		// TODO Auto-generated method stub
		List<TestItemDO> list = this.getSqlMapClientTemplate().queryForList(
				"basicData.getChangeTypeTestItems", changeId);
		return list;
	}

	@Override
	public List<TestItemDO> getNotChangeTypeTestItems(Integer changeId) {
		// TODO Auto-generated method stub
		List<TestItemDO> list = this.getSqlMapClientTemplate().queryForList(
				"basicData.getNotChangeTypeTestItems", changeId);
		return list;
	}

	@Override
	public void saveChangeTypeTestItems(Integer changeTypeId, String[] testItems) {
		// TODO Auto-generated method stub
		// 删除所有分配给变更类型的实验项目
		this.getSqlMapClientTemplate().delete("basicData.deleteChangeTypeTestItems", changeTypeId);
		// 插入实验项目
		for(String testItem:testItems ){
			Map<String, String> params = new HashMap<String, String>();
			params.put("changeTypeId", String.valueOf(changeTypeId));
			params.put("testItemId", testItem);
			this.getSqlMapClientTemplate().insert("basicData.addChangeTypeTestItem", params);
		}
	}

	@Override
	public List<ChangeTypeTestItemsDO> getTestItemssByChangeId(Integer changeId) {
		// TODO Auto-generated method stub
		List<ChangeTypeTestItemsDO> list = this.getSqlMapClientTemplate().queryForList(
				"basicData.getTestItemsByChangeId", changeId);
		
		for(ChangeTypeTestItemsDO changeTypeTestItemsDO:list){
			changeTypeTestItemsDO.setIsMustString(OK_MAP.get(changeTypeTestItemsDO.getIsMust()));
		}
		
		return list;
	}

	@Override
	public void updateIsMust(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("basicData.updateIsMust", params);
	}

	@Override
	public void isPublish(Map<String, String> params) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("basicData.isPublish", params);
	}
}
